// pages/addHouse/chooseTag/chooseTag.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imgArray:[],
    index: 1,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var imgArray =  JSON.parse(options.imgArray);
    console.log(imgArray)
      this.setData({
        imgArray:imgArray
      })
      this.setData({
        index: Number(options.key)+1
      })
  },
  //切换
  onSlideChangeEnd: function (e) {
      var that = this;
      that.setData({
        index: e.detail.current + 1
      })
    },
    //删除图哦
    deletImg(e){
      var that = this;
      wx.showModal({
        title: '确定要删除该图片吗？',
        success:function(res){
         if(res.confirm){
           var imgArray = that.data.imgArray;
           var index = that.data.index-1;
           if(index == imgArray.length - 1){
             that.setData({
               index: index
             })
           }
            var newImgArray = imgArray.splice(index,1);
            if(imgArray.length==0){
              wx.navigateBack({
                detal:0
              })
            }else{
              that.setData({
                imgArray:imgArray
              })
            }
         }
        }
      })
    },
    selectImgType(e){
      var index = this.data.index-1
      var imgArray = this.data.imgArray;
      var imgtypecode = e.currentTarget.dataset.imgtypecode;
      var imgtypename = e.currentTarget.dataset.imgtypename;
      imgArray[index].tagName=imgtypename;
      imgArray[index].tagCode=imgtypecode;
      this.setData({
        imgArray:imgArray
      })
    },
    //设为封面
    setCover(){
      var index = this.data.index-1;
      var imgArray = this.data.imgArray;
      for(let i =0;i<imgArray.length;i++){
        imgArray[i].cover = false;
      }
      console.log(index, imgArray)
      imgArray[index].cover = true;
      console.log(index, imgArray)

      this.setData({
        imgArray:imgArray
      })
    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
      var that = this;
      var pages = getCurrentPages();
      var currPage = pages[pages.length - 1];   //当前页面
      var prevPage = pages[pages.length - 2];  //上一个页面
      prevPage.setData({
        imgArray: that.data.imgArray
      })
      prevPage.selectComponent('#drag').dataChange();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  }
})
